﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Xml.Serialization;
using EPiServer.Core;
using EPiServer.PlugIn;

namespace EPiServer.Municipality.Web.UI.PropertyControls {
    /// <summary>
    /// Custom PropertyData implementation
    /// </summary>
    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName = "Sidan granskad")]
    public class PropertyReviewed : PropertyData {
        /* *******************************************************************
         *  Properties
         * *******************************************************************/
        #region public DateTime Date
        /// <summary>
        /// Get/Sets the Date of the PropertyReviewed
        /// </summary>
        /// <value></value>
        [XmlIgnore]
        public DateTime Date {
            get {
                return _date;
            }
            set {
                ThrowIfReadOnly();
                if ((_date != value) || IsNull) {
                    _date = value;
                    Modified();
                }
            }
        }
        private DateTime _date;
        #endregion
        #region public override object Value
        /// <summary>
        /// Get/Sets the Value of the PropertyReviewed
        /// </summary>
        /// <value></value>
        public override object Value {
            get {
                if (IsNull)
                    return null;
                return Date;
            }
            set {
                if (value == null)
                    Clear();
                else
                    Date = Convert.ToDateTime(value);
            }
        }
        #endregion
        #region public override Type PropertyValueType
        /// <summary>
        /// Gets the PropertyValueType of the PropertyReviewed
        /// </summary>
        /// <value></value>
        public override Type PropertyValueType {
            get {
                return typeof(DateTime);
            }
        }
        #endregion
        #region public override EPiServer.Core.PropertyDataType Type
        /// <summary>
        /// Gets the Type of the PropertyReviewed
        /// </summary>
        /// <value></value>
        public override PropertyDataType Type {
            get {
                return PropertyDataType.Date;
            }
        }
        #endregion
        /* *******************************************************************
         *  Methods
         * *******************************************************************/
        #region public override PropertyData ParseToObject(string str)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public override PropertyData ParseToObject(string str) {
            return Parse(str);
        }
        #endregion
        #region public static PropertyDate Parse(string str)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static PropertyDate Parse(string str) {
            PropertyDate date;
            if (QualifyAsNullString(str)) {
                return new PropertyDate();
            }
            try {
                date = new PropertyDate(Convert.ToDateTime(str));
            } catch (FormatException ex) {
                throw new InvalidPropertyValueException(typeof(PropertyDate).ToString(), str, ex);
            }
            return date;
        }
        #endregion
        #region public override void ParseToSelf(string str)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        public override void ParseToSelf(string str) {
            Value = Parse(str).Value;
        }
        #endregion
        #region protected override void SetDefaultValue()
        /// <summary>
        /// 
        /// </summary>
        protected override void SetDefaultValue() {
            ThrowIfReadOnly();
        }
        #endregion
        #region public override EPiServer.Core.IPropertyControl CreatePropertyControl()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IPropertyControl CreatePropertyControl() {
            return new PropertyReviewedControl();
        }
        #endregion
    }
}